﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PDX.ContextManagement
{
    /// <summary>
    /// Responsible for creating the <see cref="IPropertyModel"/>
    /// and keeping it in sync with the <see cref="IPropertyResolution"/>.
    /// </summary>
    public class PropertyModelManager
    {
        Context Target { get; set; }
        IPropertyModelFactory PropertyModelFactory { get; set; }
        DefaultPropertyModel PropertyModel { get; set; }
        Notification.INotificationManager notifier = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyModelManager"/> class.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="propModelFactory">The prop model factory.</param>
        /// <param name="notifier">The notifier.</param>
        public PropertyModelManager(Context target, IPropertyModelFactory propModelFactory,
            Notification.INotificationManager notifier)
        {
            this.Target = target;
            this.PropertyModelFactory = propModelFactory;
            this.notifier = notifier;

            CreatePropertyModel();

            target.DependencyPropertyChanged += target_DependencyPropertyChanged;
            notifier.NotificationRaised += notifier_PropertyChanged;
        }

        void target_DependencyPropertyChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
        {
            if (e.Property == Context.ResolutionProperty)
            {
                Update();
            }
        }

        void CreatePropertyModel()
        {
            PropertyModel = PropertyModelFactory.CreatePropertyModel(Target) as DefaultPropertyModel;
            if (PropertyModel == null)
            {
                throw new Exception("Invalid PropertyModel type");
            }
            Target.SetValue(Context.PropertyModelProperty, PropertyModel);
        }

        void notifier_PropertyChanged(object sender, EventArgs e)
        {
            PropertyModel.CheckPropChanged();
        }

        void Update()
        {
            var resolution = Target.GetResolution();
            if (resolution == null)
            {
                return;
            }

            PropertyModel.DisplayName = GetDisplayName(resolution);
            PropertyModel.PropertyType = resolution.Property.PropertyType;

            PropertyModel.Getter = () => resolution.Property.GetValue(resolution.ResolvedDC, null);
            PropertyModel.Setter = (value) =>
                {
                    using (notifier.BeginDisposableTransaction())
                    {
                        resolution.Property.SetValue(resolution.ResolvedDC, value, null);
                    }
                };

            PropertyModel.Ext["OriginalDC"] = resolution.ResolvedDC;
        }

        /// <summary>
        /// Determines the display name given the resolution
        /// </summary>
        /// <param name="resolution">The resolution.</param>
        /// <returns></returns>
        protected virtual string GetDisplayName(IPropertyResolution resolution)
        {
            var prop = resolution.Property;

            var displayNameAtt = prop.GetSingleAttribute<System.ComponentModel.DisplayNameAttribute>();
            if (displayNameAtt != null)
            {
                return displayNameAtt.DisplayName;
            }

            var displayAtt = prop.GetSingleAttribute<System.ComponentModel.DataAnnotations.DisplayAttribute>();
            if (displayAtt != null)
            {
                return displayAtt.Name;
            }

            return prop.Name;
        }
    }
}
